<template>
  <div class="mod-config">
    <basic-container>
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="商品标题" prop="spuTitle">
          <el-input
            v-model.trim="queryParams.spuTitle"
            placeholder="请输入商品标题"
            clearable
            size="small"
            @keyup.enter.native="getDataList"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getDataList"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-button
            v-if="permissions.goods_spu_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle()"
            >新增</el-button
          >
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" 
                  border 
                  v-loading="dataListLoading"
                  row-key="id"
                  :tree-props="{children: 'children', hasChildren: 'hasChildrens'}">
          <el-table-column
            type="index"
            header-align="center"
            :index="indexMethod"
            align="center"
            label="编号"
          >
          </el-table-column>
          <el-table-column
            prop="spuTitle"
            header-align="center"
            align="center"
            label="商品标题"
            width="120px"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="商品类型"
            header-align="center"
            align="center"
            width="100">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.spuType === '1'">普通</el-tag>
              <el-tag type="success" v-else-if="scope.row.spuType === '2'">内购</el-tag>
              <el-tag type="success" v-else>隐藏</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="商品sku信息" align="center" width="930">
            <template slot-scope="scope">
              <el-table
                :data="scope.row.skus">
                <el-table-column
                  prop="skuShortName"
                  header-align="center"
                  label="商品sku简称"
                  align="center"
                  width="180">
                </el-table-column>
                <el-table-column
                  prop="name"
                  label="上架状态"
                  header-align="center"
                  align="center"
                  width="100">
                  <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.status === '1'">上架</el-tag>
                    <el-tag type="info" v-else>下架</el-tag>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="canningCount"
                  header-align="center"
                  align="center"
                  label="罐装数量"
                  width="100">
                </el-table-column>
                <el-table-column
                  prop="bagCount"
                  header-align="center"
                  align="center"
                  label="袋装数量"
                  width="100">
                </el-table-column>
                <el-table-column
                  prop="stripCount"
                  header-align="center"
                  align="center"
                  label="条装数量"
                  width="100">
                </el-table-column>
                <el-table-column
                  header-align="center"
                  align="center"
                  label="首页一级推荐"
                  width="100">
                  <template slot-scope="scope">
                    <span v-if="scope.row.hasMain == '1'">是</span>
                    <span v-else>否</span>
                  </template>
                </el-table-column>
                <el-table-column
                  header-align="center"
                  align="center"
                  label="首页二级推荐"
                  width="100">
                  <template slot-scope="scope">
                    <span v-if="scope.row.hasSubMain == '1'">是</span>
                    <span v-else>否</span>
                  </template>
                </el-table-column>
                <el-table-column
                  header-align="center"
                  align="center"
                  label="推荐列表"
                  width="100">
                  <template slot-scope="scope">
                    <span v-if="scope.row.hasRecommend == '1'">是</span>
                    <span v-else>否</span>
                  </template>
                </el-table-column>
                <el-table-column header-align="center" align="center" label="操作" width="120px">
                  <template slot-scope="scope">
                    <el-button
                      v-if="permissions.goods_sku_edit"
                      type="text"
                      size="small"
                      icon="el-icon-edit"
                      @click="handleSkuEdit(scope.row)"
                      >修改</el-button>
                    <el-button
                      v-if="permissions.goods_sku_edit"
                      type="text"
                      size="small"
                      icon="el-icon-edit"
                      @click="handleSkuStatus(scope.row)"
                      >
                        <span v-if="scope.row.status === '1'">下架</span>
                        <span v-else>上架</span>
                      </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>

          <el-table-column
            prop="sort"
            header-align="center"
            align="center"
            label="排序"
            width="100"
          >
          </el-table-column>

          <el-table-column
            prop="createTime"
            header-align="center"
            align="center"
            label="创建时间"
            width="140px"
          >
          </el-table-column>

          <el-table-column header-align="center" align="center" label="操作" width="120px">
            <template slot-scope="scope">
              <el-button
                v-if="permissions.goods_spu_edit"
                type="text"
                size="small"
                icon="el-icon-edit"
                @click="addOrUpdateHandle(scope.row.id)"
                >修改</el-button>
              <el-button
                v-if="permissions.goods_spu_del"
                type="text"
                size="small"
                icon="el-icon-delete"
                @click="deleteHandle(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="queryParams.pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryParams.pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <EditForm
        v-if="visible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
        :visible.sync="visible"
      ></EditForm>

      <SkuEditForm
        v-if="skuVisible"
        ref="skuEditForm"
        @refreshDataList="getDataList"
        :visible.sync="skuVisible"
      ></SkuEditForm>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj, updateSkuStatusById} from "@/api/shop/goods/spu";
import EditForm from "./edit";
import SkuEditForm from './sku_edit';
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        spuTitle:''
      },
      dataList: [],

      totalPage: 0,
      dataListLoading: false,
      visible: false,
      skuVisible: false,
    };
  },
  components: {
    EditForm,
    SkuEditForm
  },
  created() {
    this.getDataList();
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    /**
     * 获取数据列表
     */
    getDataList() {
      this.dataListLoading = true;
      fetchList(
        Object.assign({
          spuTitle: this.queryParams.spuTitle,
          current: this.queryParams.pageIndex,
          size: this.queryParams.pageSize
        })
      ).then((response) => {
        this.dataListLoading = false;
        this.dataList = response.data.data.records;
        this.totalPage = response.data.data.total;
      });
    },

    /**
     * 每页数
     */
    sizeChangeHandle(val) {
      this.queryParams.pageSize = val;
      this.queryParams.pageIndex = 1;
      this.getDataList();
    },

    /**
     * 当前页
     */
    currentChangeHandle(val) {
      this.queryParams.pageIndex = val;
      this.getDataList();
    },

    /**
     * 新增 / 修改
     */
    addOrUpdateHandle(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },

    /**
     * 修改Sku
     */
    handleSkuEdit(sku){
      this.skuVisible = true;
      this.$nextTick(() => {
        this.$refs.skuEditForm.init(sku.spuId ,sku.id, sku.skuShortName);
      });
    },

    /**
     * sku上下架
     */
    handleSkuStatus(sku){
      console.log(sku)
      let msg = ''
      if(sku.status === '1'){
        msg = '是否下架当前sku'
      } else {
        msg = '是否上架当前sku'
      }
      this.$confirm(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          let data = {}
          data.id = sku.id
          if(sku.status === '1'){
            data.status = '0'
          } else {
            data.status = '1'
          }
          // 避免sku上下架，策略丢失
          data.pointPolicyId = sku.pointPolicyId
          data.firstProfitPolicyId = sku.firstProfitPolicyId
          data.secondProfitPolicyId = sku.secondProfitPolicyId
          return updateSkuStatusById(data);
        })
        .then((data) => {
          this.$message.success("状态修改成功");
          if(this.dataList.length <= 1 && this.queryParams.pageIndex > 1){
            this.queryParams.pageIndex--;
          }
          this.getDataList();
        })
        .catch(() => {});
    },

    /**
     * 删除
     */
    deleteHandle(id) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          if(this.dataList.length <= 1 && this.queryParams.pageIndex > 1){
            this.queryParams.pageIndex--;
          }
          this.getDataList();
        })
        .catch(() => {});
    },

    /**
     * 重置
     */
    resetQuery() {
      this.queryParams.pageIndex = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.getDataList();
    },

    indexMethod(index) {
      return (
        (this.queryParams.pageIndex - 1) * this.queryParams.pageSize + index + 1
      );
    },
  },
};
</script>
